package net.soe;

import net.soe.__Shape._;
import net.soe.__Region._;
import net.soe.__Picture._;

import java.awt.event.ActionEvent;
import java.awt.geom.Area;
import scala.Math._;
import java.awt.Graphics2D;
import javax.swing._
import java.awt.event._

object __Animation {

	
type time=Double
type Animation[a]= (time) => a

def rubberBall:Animation[_Shape] = {
	println("Got to rubberBall")
		t => Ellipse (sin(t), cos(t))
}


def revolvingBall:Animation[_Region] = { t =>
		val ball = Shape(Ellipse(0.2, 0.2))
		Translate((sin(t), cos(t)), ball)
}

def planets: Animation[_Picture] = { t=>
      val p1 = Region(Red, Shape(rubberBall(t)))
     val p2 = Region(Yellow, revolvingBall(t))
     p1 Over p2
}

def tellTime:Animation[String] = { t =>
	"The time is" + t
}

      
def animate( title: String, color: _Color, anim:Animation[Area] ) {
    		 SwingUtilities.invokeAndWait(new AnimationPanel(title, color, anim))
}
 
      
class AnimationPanel(title: String, color: _Color, anim:Animation[Area] ) extends BasePanel(title)  
            with ActionListener  {
	
    	  override def doPaint(g: Graphics2D)  {
    		 println("GOt to doPaint")
   			 withColor( g, color, anim(System.currentTimeMillis())) 
          }
    
    	  def actionPerformed(e: ActionEvent) {
    		  repaint()
    	  }
    	  
    	  override def run() {
//    		  // NOTE: Picture doesn't have any reactive features, ActionListner and Timer listener are not defined.
//    		    setPreferredSize(new Dimension(xWin, yWin))
//    		    setFocusable(true)
//    		    val frame = new JFrame(title)
//    		    frame.setLayout(new BorderLayout())
//    		    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
//    		    frame.setResizable(false)
//    		    frame.add(this, BorderLayout.CENTER)
//    		    frame.pack()
//    		    frame.setVisible(true)
  		      
    		  new Timer(50, this).start()

    		  super.run()
    	   }
       }

def main(args: Array[String]) {
  animate ("Animated Shape", Blue, rubberBall andThen shapeToArea)

}
}